package com.ellwood.IPGuard.business.system.logic;

import com.ellwood.IPGuard.business.system.convert.UserConvert;
import com.ellwood.IPGuard.common.core.basic.LogicFactory;
import com.ellwood.IPGuard.common.core.enums.HttpCodeEnum;
import com.ellwood.IPGuard.common.core.exception.SystemException;
import com.ellwood.IPGuard.common.core.utils.EllStrUtils;
import com.ellwood.IPGuard.infrastruction.domain.entity.system.UmsSysUserEntity;
import com.ellwood.IPGuard.infrastruction.domain.query.UmsSysUserQuery;
import com.ellwood.IPGuard.infrastruction.domain.vo.system.user.UmsSysUserVO;
import com.ellwood.IPGuard.infrastruction.repository.system.UmsSysUserRepository;
import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.business.system.logic
 * @ClassName: UmaSysUserLogic
 * @Description: // 用户逻辑业务处理
 * @Author: Ellwood
 * @CreateDate: 2024/9/26 - 上午11:34
 * @Version: V1.0.0
 */
@AllArgsConstructor
public class UmaSysUserLogic extends LogicFactory<UmsSysUserEntity> {
        
        private final UmsSysUserRepository userRepository;
        
        /**
         * 获取查询参数
         *
         * @param userQuery 查询参数
         * @return 返回参数
         */
        public static Map<String, Object> getPageParams(UmsSysUserQuery userQuery) {
                Map<String, Object> params = new HashMap<>();
                params.put("username", userQuery.getUsername());
                params.put("nickname", userQuery.getNickname());
                params.put("mobile", userQuery.getMobile());
                params.put("email", userQuery.getEmail());
                params.put("sex", userQuery.getSex());
                return params;
        }
        
        /**
         * 获取新增用户信息
         *
         * @param userVO 新增用户参数
         */
        public UmsSysUserEntity getAddUser(UmsSysUserVO userVO) {
                
                UmsSysUserEntity entity = UserConvert.INSTANCE.convertVoTOEntity(userVO);
                
                // 新增密码不能为空
                if (EllStrUtils.isBlank(userVO.getPassword()))
                        throw new SystemException(HttpCodeEnum.PASSWORD_NOT_NULL);
                
                // 新增用户账号不能为空
                if (EllStrUtils.isBlank(userVO.getUsername()))
                        throw new SystemException(HttpCodeEnum.USERNAME_NOT_NULL);
                
                // 新增用户手机号不能为空
                if (EllStrUtils.isBlank(userVO.getMobile()))
                        throw new SystemException(HttpCodeEnum.MOBILE_NUMBER_NOT_NULL);
                
                // 验证账号手机号是否重复
                userVerifyRepeat(userVO, entity);
                
                return entity;
        }
        
        /**
         * 获取更新用户信息
         *
         * @param userVO 更新用户参数
         * @return 更新用户信息
         */
        public UmsSysUserEntity getUpdateUser(UmsSysUserVO userVO) {
                UmsSysUserEntity entity = UserConvert.INSTANCE.convertVoTOEntity(userVO);
                
                userVerifyRepeat(userVO, entity);
                
                return entity;
        }
        
        /**
         * 验证用户账号和手机号是否重复
         *
         * @param userVO 用户vo
         * @param entity 用户实体
         */
        private void userVerifyRepeat(UmsSysUserVO userVO, UmsSysUserEntity entity) {
                // 判断用户名是否存在
                UmsSysUserEntity username = userRepository.getUserByUsername(userVO.getUsername());
                if (username != null && !username.getId().equals(entity.getId()))
                        throw new SystemException(HttpCodeEnum.USERNAME_EXIST);
                
                // 判断手机号是否存在
                UmsSysUserEntity mobile = userRepository.getUserByMobile(userVO.getMobile());
                if (mobile != null && !mobile.getId().equals(entity.getId()))
                        throw new SystemException(HttpCodeEnum.MOBILE_NUMBER_EXIST);
        }
}
